package com.hinnotek.warehouse.binding

import android.graphics.Color
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.github.chrisbanes.photoview.PhotoView
import com.hinnotek.warehouse.PhotoViewImageViewTarget
import com.lee.common.utils.StringUtils
import com.lee.common.widget.CornerLabelView
import com.xuexiang.xui.widget.imageview.preview.loader.GlideMediaLoader
import com.xuexiang.xui.widget.textview.ExpandableTextView


object BindingViewExt {

    @JvmStatic
    @BindingAdapter("loadImageUrl")
    fun loadImageUrl(view: ImageView, url: String?) {
        if (url.isNullOrEmpty()) {
            return
        }
        Glide.with(view.context)
            .load(url)
            .apply(GlideMediaLoader.getRequestOptions())
            .into(view)
        view.setTag(view.id, url)
    }

    @JvmStatic
    @BindingAdapter("loadImage")
    fun loadImage(imageView: ImageView, @DrawableRes id: Int?) {
        if (id != null)
            imageView.setImageResource(id)
    }

    @JvmStatic
    @BindingAdapter("loadPhotoImage")
    fun loadPhotoImage(view: PhotoView, url: String?) {
        Glide.with(view.context)
            .asDrawable()
            .load(url)
            .apply(GlideMediaLoader.getRequestOptions())
            .into(PhotoViewImageViewTarget(view))
    }

    @JvmStatic
    @BindingAdapter("toDBCString")
    fun toDBCString(view: TextView, content: String?) {
        if (content == null)
            return
        view.setText(StringUtils.toDBC(content))
    }

    @JvmStatic
    @BindingAdapter("isOver20")
    fun isOver20(view: TextView, content: String?) {
        if (content == null)
            return
        view.text = content
        val num = content.split("：")[1]
        if (num.toInt() < 20) {
            view.setTextColor(Color.RED)
        }
    }


    @JvmStatic
    @BindingAdapter("setExpandText")
    fun setExpandText(view: ExpandableTextView, content: String?) {
        if (content == null)
            return
        view.text = StringUtils.toDBC(content)
    }

    @JvmStatic
    @BindingAdapter("badgeText")
    fun setBadgeText(view: CornerLabelView, content: String?) {
        if (content == null)
            return
        view.setText(content)
    }

}